package jh.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast

import com.bumptech.glide.Glide
import com.jh.ios.forecast.R

import org.apache.cordova.FileManager
import org.json.JSONObject

import java.lang.reflect.Array
import java.util.ArrayList
import java.util.HashMap

import jh.interfaces.OnItemPictureClickListener
import jh.model.CModel
import jh.model.UserModel
import jh.utils.HttpUtils
import jh.utils.NoDoubleClickListener
import jh.view.NineGridTestLayout

class CommunityAdapter(private val context: Context, private val datas: List<CModel>, private val listener: OnItemPictureClickListener) : BaseAdapter() {
    private val inflater: LayoutInflater
    private val hideIcon: Boolean? = null

    init {
        this.inflater = LayoutInflater.from(context)
    }


    override fun getCount(): Int {
        return datas.size
    }

    override fun getItem(position: Int): Any? {
        return null
    }

    override fun getItemId(position: Int): Long {
        return 0
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var convertView = convertView

        var viewHolder: ViewHolder? = null
        if (convertView == null) {


            convertView = inflater.inflate(R.layout.layout_community_item, null)
            viewHolder = ViewHolder(convertView)
            convertView!!.tag = viewHolder
        } else {
            viewHolder = convertView.tag as ViewHolder
        }

        val model = datas[position]
        if (model.photo.isNullOrBlank()){  //头像url为空

        }else{
            Glide.with(context).load(model.photo).into(viewHolder.avatar)
        }
        viewHolder.name?.text = model.nickName
        viewHolder.level?.text = model.gradeName
        viewHolder.tag?.text = model.lotteryCategoryName
        viewHolder.title?.text = model.content   //去掉内容，只要标题，标题显示原先内容信息
        //        viewHolder.content.setText(model.getContent());
        viewHolder.time?.text = model.createTime
        viewHolder.praise?.text = model.likeNumber.toString() + ""
        viewHolder.commentBtn?.setOnClickListener {
            lis?.let {
                it.onClick(model)
            }
        }
        viewHolder.praiseBtn?.setOnClickListener(object : NoDoubleClickListener() {
            override fun onNoDoubleClick(v: View?) {
                 praiseAction(model)
            }
        })

        val path = FileManager.getFilePath() + "/www/assets/vip/"
        if (model.gradeCode - 1 > 0) {
            val vipImageName = path + "/vip" + (model.gradeCode - 1) + ".png"
            Glide.with(context).load(vipImageName).into(viewHolder.vip)
            viewHolder.vip?.visibility = View.VISIBLE
        } else {
            viewHolder.vip?.visibility = View.GONE
        }
        if (model.isDz == 1) {
            viewHolder.dz?.setImageDrawable(context.resources.getDrawable(R.mipmap.press))
        } else {
            viewHolder.dz?.setImageDrawable(context.resources.getDrawable(R.mipmap.press_norme))
        }
        var imgModel=model.imgs
        if (imgModel==null) {
            viewHolder.nineTestlayout?.visibility=View.GONE
        } else {

            if (imgModel.endsWith(",")) imgModel=imgModel.substring(0,imgModel.length-1)

            viewHolder.nineTestlayout?.visibility=View.VISIBLE

            viewHolder.nineTestlayout?.setListener(listener)

            viewHolder.nineTestlayout?.setItemPosition(position)
            viewHolder.nineTestlayout?.setIsShowAll(false) //当传入的图片数超过9张时，是否全部显示
            viewHolder.nineTestlayout?.setSpacing(5f) //动态设置图片之间的间隔
            viewHolder.nineTestlayout?.setUrlList(imgModel!!.split(","))

        }
            viewHolder.comment?.text="${model.count}"  //点赞条数
        return convertView
    }

    private fun praiseAction(model: CModel) {

        val userModel = UserModel.getUserInfo(context)
        val map = HashMap<String, String>()
        map.put("id", model.id.toString() + "")
        map.put("loginToken", userModel.loginToken)
        if (model.isDz == 1) {
            val url = "/api/communicationcircle/qxDz"

            HttpUtils.POST(url, map) { `object`, msg, e ->
                if (e == null) {
                    if (msg == null) {
                        model.likeNumber = model.likeNumber - 1
                        model.isDz = 0
                        notifyDataSetChanged()
                    }
                }
            }
        } else {
            val url = "/api/communicationcircle/updateLike"
            HttpUtils.POST(url, map) { `object`, msg, e ->
                if (e == null) {
                    if (msg == null) {
                        model.likeNumber = model.likeNumber + 1
                        model.isDz = 1
                        notifyDataSetChanged()
                    }
                }
            }
        }
    }


    inner class ViewHolder(view: View) {
        var avatar: ImageView? = null
        var vip: ImageView? = null
        var dz: ImageView? = null
        var name: TextView? = null
        var level: TextView? = null
        var tag: TextView? = null
        var title: TextView? = null
        //        public TextView content;
        var time: TextView? = null
        var comment: TextView? = null
        var praise: TextView? = null
        var commentBtn: View? = null
        var praiseBtn: View? = null

        var nineTestlayout: NineGridTestLayout? = null

        init {
            avatar = view.findViewById(R.id.avatar)
            vip = view.findViewById(R.id.vip_icon)
            name = view.findViewById(R.id.name)
            level = view.findViewById(R.id.level)
            tag = view.findViewById(R.id.tag)
            title = view.findViewById(R.id.title)
            //            content = view.findViewById(R.id.content);
            time = view.findViewById(R.id.time)
            comment = view.findViewById(R.id.comment)
            praise = view.findViewById(R.id.praise)
            praiseBtn = view.findViewById(R.id.praiseBtn)
            commentBtn = view.findViewById(R.id.commentBtn)
            dz = view.findViewById(R.id.dz)
            nineTestlayout = view.findViewById(R.id.nineTestlayout)
        }

    }

//    var listner: ((itemBean: CModel) -> Unit)? = null
//
//    fun setMyListner(listner: (itemBean: CModel) -> Unit) {
//        this.listner = listner
//    }

    var lis: Listener<CModel>? = null

    interface Listener<CModel> {
        fun onClick(a: jh.model.CModel)
    }

    fun setaaListener(listener: Listener<CModel>) {
        this.lis = listener
    }

}
